package br.jcrsistemas.database;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import br.jcrsistemas.syscomercio.bean.CategoriaProduto;
import br.jcrsistemas.syscomercio.bean.Comanda;
import br.jcrsistemas.syscomercio.bean.Configuracao;
import br.jcrsistemas.syscomercio.bean.Entidade;
import br.jcrsistemas.syscomercio.bean.PessoaFisica;
import br.jcrsistemas.syscomercio.bean.PessoaJuridica;
import br.jcrsistemas.syscomercio.bean.Produto;
import br.jcrsistemas.util.Configuracoes;
import br.jcrsistemas.util.Util;

public class HibernateUtility {
    private static final SessionFactory sessionFactory;
    private static final Conexao conn = ConexaoFactory.getConnectionFactory();

    static {
        try {
            Configuration cfg = new Configuration()
            .setProperty("hibernate.dialect", conn.getDialeto())
            .setProperty("hibernate.connection.driver_class", conn.getDriverClass())
            .setProperty("hibernate.connection.url", conn.getURLConexao())
            .setProperty("hibernate.connection.username", Configuracoes.getUsuarioBanco())
            .setProperty("hibernate.connection.password", Configuracoes.getSenhaBanco())
            .setProperty("hibernate.hbm2ddl.auto", "true")
            .setProperty("hibernate.connection.autocommit", "false")
            .setProperty("hibernate.show_sql", Configuracoes.isDebugOn() ? "true" : "false")
            .addAnnotatedClass(CategoriaProduto.class)
            .addAnnotatedClass(Produto.class)
            .addAnnotatedClass(Entidade.class)
            .addAnnotatedClass(PessoaJuridica.class)
            .addAnnotatedClass(Configuracao.class)
            .addAnnotatedClass(Comanda.class)
            .addAnnotatedClass(PessoaFisica.class);
//            gerarTabelas(cfg);
            sessionFactory = cfg.buildSessionFactory();
//            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static Session getSessao() {
    	return sessionFactory.openSession();
    }
    
    public static void gerarTabelas(Configuration cfg) {
    	new SchemaExport(cfg).create(true, true);
    	
    }
}



